<template>
  <div>
    <!-- data表格的数据 -->
    <!--default-expand-all 默认展开所有子菜单 -->
    <!--tree-props 树形结构的选项 -->
   <div class="bigBox">
      <el-table class="smBox"
      :data="tableData"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <!-- prop:数据的字段名 -->
      <!-- label:标签名 -->
      <!-- sortable:是否可以排序 -->
      <el-table-column prop="id" label="ID" width="80"> </el-table-column>
      <el-table-column prop="title" label="标题" width="180"> </el-table-column>
      <el-table-column prop="type" label="类型">
        <template slot-scope="scope">
          <!-- 这里需要type这个变量，说以用到了作用域插槽来传递变量 -->
          <!-- slot-scope表示是作用域插槽，scope就是数据，就是循环中的item -->
          <el-tag v-if="scope.row.type === 1" type="success">目录</el-tag>
          <el-tag v-else type="danger">菜单</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="icon" label="图标">
        <template slot-scope="scope">
          <i v-if="scope.row.icon" :class="scope.row.icon"></i>
        </template>
      </el-table-column>
      <el-table-column prop="url" label="地址"> </el-table-column>
      <el-table-column prop="status" label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 1" type="success">正常</el-tag>
          <el-tag v-else type="danger">禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-edit"
            circle
            @click="edit(scope.row.id)"
          ></el-button>
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            circle
            @click="del(scope.row.id)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
   </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
    };
  },
  created() {
    this.getMenuListtree();
  },
  methods: {
    // 获取菜单的树形数据
    getMenuListtree() {
      this.$http("/menulist?istree=1")
        .then((res) => {
          //   console.log(res);
          // 根据接口数据，赋值
          this.tableData = res.list;
        })
        .catch((err) => {
          console.log(err);
        });
    },
    // 编辑菜单
    edit(id) {
      this.$router.push('/menu/'+id)
    },
    // 删除菜单弹窗
    del(id) {
      // 弹窗，二次确认
      this.$confirm("确定删除吗, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        // 点击确认按钮
        .then(() => {
          // 真的要删了
          this.delMenu(id);

        })
        // 点击取消按钮
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
   
    },
    delMenu(id){
   this.$http.post("/menudelete", { id }).then((res) => {
    //  删除的接口会返回新的数据
      this.tableData = res.list;
        // 删除成功后提示
        this.$message({
            type: "success",
            message: "删除成功!",
          });
      });
    }
  },
};
</script>

<style>
  .bigBox{
    overflow: hidden;
    margin-top: 18px;
    width: auto;
    height: auto;
    background-color: #FFF;
  }
  .bigBox .smBox{
    margin: 8px 18px 19px 18px;
  }
</style>